<template>
  <!-- 查询客户 -->
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="3"
      labelWidth="80"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      selectionType="single"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
    ></FilterTable>
    <!-- <div slot="footer" align="center">
      <el-button @click="saveDialog" type="primary">确 定</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div> -->
		<div
      slot="footer"
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">返 回</el-button>
      <el-button @click="saveDialog" type="primary">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { getEmpInfo, queryPotentialById } from '@/api/potenCus/customer/driveManagement';
import {
  potentialCustomer,
  queryEmployees
 } from '@/api/potenCus/customer/quotedprice';
import { dictionary } from '@/utils/dictMixin';
export default {
  name: 'SearchName',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      topFilterList: this.createTopFilters(),
      fetchParams: {
        limit: 5,
        pageNum: 1,
      },
      visible: false,
      dialogTitle: '',
      fetchApi: potentialCustomer,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      conf: { search: '线索选择' },
      selectedRow: null
    };
  },
  created() {
    this.init();
    this.getEmployeeList();
  },
  methods: {
    init() {
      if (this.type === 'edit' || this.type === 'show') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.basedataId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    selectChangeHandle(rows) {
      if (!rows.length) return;
      this.selectedRow = rows[0];
    },
    createTopFilters() {
      return [
        {
          type: 'SELECT',
          label: '销售顾问',
          fieldName: 'salesConsultant',
          placeholder: '请选择',
          initialValue: '',
          itemList: [],
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'potentialCustomerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '客户手机号',
          fieldName: 'potentialCustomerPhone',
          placeholder: '请输入'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '客户编号',
          width: 130,
          dataIndex: 'potentialCustomerNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户名称',
          dataIndex: 'potentialCustomerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '客户来源',
          dataIndex: 'clueName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '意向级别',
          dataIndex: 'intent',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1519'),
          render: props => {
            return <span>{this.createDictText(props.row.intent, '1519')}</span>;
          }
        },
        {
          title: '手机号码',
          dataIndex: 'mobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售顾问',
          dataIndex: 'consultantName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '了解途径',
          dataIndex: 'basedataName',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    saveDialog() {
      this.closeDialog(this.selectedRow);
    },
    closeDialog(data) {
      this.$emit('close', false, data);
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    async getEmployeeList() {
      const res = await queryEmployees();
      // console.log('销售顾问', res)
      if (res.resultCode === 200) {
        this.topFilterList[0].itemList = res.data.map(x => ({ value: x.employeeNo, text: x.employeeName }));
        // this.topFilterList = [...this.topFilterList]
      }
    },
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
</style>
